﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SMWJamecl.BusinessEntities;
using SMWJamecl.Repository;

namespace SMWJamecl.Repository
{
    public class MarcaRepository:IMarcaRepository
    {
        SMWJameclContext context;
        public MarcaRepository()
        {
            if (context == null)
            {
                context = new SMWJameclContext();
            }
        }

        public List<Marca> GetAllFromMarcas()
        {
            var query = from c in context.Marcas
                        select c;
            return query.ToList();
        }


        public Marca GetAllFromMarcaById(int id)
        {
            var query = from c in context.Marcas
                        where c.Id_Marca.Equals(id)
                        select c;
            return query.SingleOrDefault();
        }

        public void AddMarca(Marca marca)
        {
            context.Marcas.Add(marca);
            context.SaveChanges();
        }

        public void UpdateMarca(Marca marca)
        {
            context.Entry(marca).State = System.Data.EntityState.Modified;
            context.SaveChanges();
        }

        public void Remove(int id)
        {
            var existe = context.Marcas.Find(id);

            if (existe != null)
            {
                context.Marcas.Remove(existe);
                context.SaveChanges();
            }
        }
    }
}
